﻿SELECT 
  p.InventLocationid                -- w.InventLocationid 
, i.idAD
, CONVERT(varchar(10), CONVERT(datetime, v.DeliveryDate), 120) as WDate
, v.ModifiedDate                    -- w.MODIFIEDDATE
, v.ModifiedTime                    -- w.ModifiedTime
, i.Name
, p.PurchId                         -- w.WMDOCUMENTREFID
, v.PackingSlipId                   -- w.WMinvoiceId
, p.PurchName                       -- w.WMcontractorName 
, SUBSTRING(p.PurchId,3,20) as NNUM -- SUBSTRING(w.WMDOCUMENTREFID,3,20) as NNUM
, p.OrderAccount as VenderID        -- w.WMCONTRACTORACCOUNTNUM as VenderID 
, p.PurchStatus
, u.NETWORKALIAS 
  FROM
    [Main].[dbo].VendPackingSlipJour v  
        INNER JOIN [Main].[dbo].PurchTable p ON p.PurchId=v.PurchId   
        LEFT OUTER JOIN [Main].[dbo].InventLocation i ON i.InventLocationId=p.InventLocationid  
        INNER JOIN [Main].[dbo].USERINFO u ON u.id=v.createdBy   
 WHERE
       ( p.PurchStatus<>4 AND v.ModifiedDate >= '2011-11-17' AND v.ModifiedTime >= '10:00:00' AND v.OrderBalance_RU >= 0 )
    OR ( p.PurchStatus<>4 AND v.ModifiedDate >= '2011-11-17' AND v.OrderBalance_RU >= 0 )
ORDER BY i.Name, CONVERT(varchar(10), CONVERT(datetime, v.DeliveryDate), 120), p.OrderAccount